package com.ailk.kbi.kbirazor.component
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import spark.components.Group;
	
	public class LinkLineGroup extends Group
	{
		private var _contentXML:XML;
		private var _nodeGroupList:ArrayCollection;
		
		public function LinkLineGroup(contentXML:XML){
			super();
			_contentXML = contentXML;
			updateChildren();
		}
		
		private function updateChildren():void{
			this.width = 30;
			this.addEventListener("heightChanged",thisChangedHandler);
		}
		
		
		public function set ancherY(value:ArrayCollection): void{
			this.removeAllElements();
			for each(var endY:Number in value){				
				var linkLine:LinkLine = new LinkLine();
				this.addElement(linkLine);				
				linkLine.xFrom = 0;
				linkLine.xTo = width;
				linkLine.yFrom = height/2;
				linkLine.yTo = endY;
				linkLine.x = 0;
				linkLine.y = Math.min(linkLine.yFrom, linkLine.yTo);				
			}
			this.toolTip = "value = " + value.toString();
		}
		
		public function set nodeGroupList(value:ArrayCollection): void {			
			this.removeAllElements();
			_nodeGroupList = value;
			for each(var element:NodeGroup in value){				
				var linkLine:LinkLine = new LinkLine();
				this.addElement(linkLine);
				element.addEventListener("yChanged", nodeGroupChangedHandler);
				element.addEventListener("heightChanged", nodeGroupChangedHandler);				
				linkLine.xFrom = 0;
				linkLine.xTo = width;				
				linkLine.x = 0;
			}
		}
		
		private function nodeGroupChangedHandler(event:Event):void {
			var element:NodeGroup = event.target as NodeGroup;
			updateNodeGroup(element);
		}
		
		private function updateNodeGroup(element:NodeGroup):void {
			var linkLine:LinkLine = this.getElementAt(_nodeGroupList.getItemIndex(element)) as LinkLine;
			linkLine.yFrom = height/2;
			linkLine.yTo = element.y + element.height/2;
			linkLine.y = Math.min(linkLine.yFrom, linkLine.yTo);
			//			linkLine.toolTip = "yTo:" + linkLine.yTo;
		}
		
		private function thisChangedHandler(event:Event): void{
			if(_nodeGroupList){
				for each(var element:NodeGroup in _nodeGroupList){
					updateNodeGroup(element);
				}
			}
		}
	}
}